package com.ruoyi.web.controller.talent;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RoleTalentPermission;
import com.ruoyi.system.service.IRoleTalentPermissionService;

/**
 * 角色人才库字段权限管理 控制器
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/talent/permission")
public class RoleTalentPermissionController extends BaseController
{
    @Autowired
    private IRoleTalentPermissionService roleTalentPermissionService;

    /**
     * 查询角色人才库字段权限列表
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoleTalentPermission roleTalentPermission)
    {
        startPage();
        List<RoleTalentPermission> list = roleTalentPermissionService.selectRoleTalentPermissionList(roleTalentPermission);
        return getDataTable(list);
    }

    /**
     * 导出角色人才库字段权限列表
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:export')")
    @Log(title = "角色人才库权限管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoleTalentPermission roleTalentPermission)
    {
        List<RoleTalentPermission> list = roleTalentPermissionService.selectRoleTalentPermissionList(roleTalentPermission);
        ExcelUtil<RoleTalentPermission> util = new ExcelUtil<RoleTalentPermission>(RoleTalentPermission.class);
        util.exportExcel(response, list, "角色人才库权限数据");
    }

    /**
     * 获取角色人才库字段权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(roleTalentPermissionService.selectRoleTalentPermissionById(id));
    }

    /**
     * 根据角色ID和表名查询权限列表
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:query')")
    @GetMapping(value = "/role/{roleId}/table/{tableName}")
    public AjaxResult getPermissionsByRoleAndTable(@PathVariable("roleId") Long roleId, 
                                                    @PathVariable("tableName") String tableName)
    {
        List<RoleTalentPermission> permissions = roleTalentPermissionService.selectPermissionsByRoleAndTable(roleId, tableName);
        return success(permissions);
    }

    /**
     * 新增角色人才库字段权限
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:add')")
    @Log(title = "角色人才库权限管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody RoleTalentPermission roleTalentPermission)
    {
        roleTalentPermission.setCreateBy(getUsername());
        return toAjax(roleTalentPermissionService.insertRoleTalentPermission(roleTalentPermission));
    }

    /**
     * 修改角色人才库字段权限
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:edit')")
    @Log(title = "角色人才库权限管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody RoleTalentPermission roleTalentPermission)
    {
        roleTalentPermission.setUpdateBy(getUsername());
        return toAjax(roleTalentPermissionService.updateRoleTalentPermission(roleTalentPermission));
    }

    /**
     * 删除角色人才库字段权限
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:remove')")
    @Log(title = "角色人才库权限管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(roleTalentPermissionService.deleteRoleTalentPermissionByIds(ids));
    }

    /**
     * 批量保存角色权限
     */
    @PreAuthorize("@ss.hasPermi('talent:permission:edit')")
    @Log(title = "角色人才库权限管理", businessType = BusinessType.UPDATE)
    @PostMapping("/batch/{roleId}/{tableName}")
    public AjaxResult batchSave(@PathVariable("roleId") Long roleId, 
                                @PathVariable("tableName") String tableName,
                                @RequestBody List<RoleTalentPermission> permissions)
    {
        for (RoleTalentPermission permission : permissions)
        {
            permission.setCreateBy(getUsername());
        }
        return toAjax(roleTalentPermissionService.batchSaveRolePermissions(roleId, tableName, permissions));
    }
}

